package com.heng.futureDemo;

import org.junit.Test;
import java.util.concurrent.*;

public class AFutureThreadDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService threadPool = Executors.newFixedThreadPool(3);

        long startTime = System.currentTimeMillis();

        FutureTask<String> futureTask1 = new FutureTask<>(() -> {
            try {TimeUnit.MILLISECONDS.sleep(500);}catch (InterruptedException e){e.printStackTrace();}
            return "task1 over";
        });
        threadPool.submit(futureTask1);

        FutureTask<String> futureTask2 = new FutureTask<>(() -> {
            try {TimeUnit.MILLISECONDS.sleep(500);}catch (InterruptedException e){e.printStackTrace();}
            return "task2 over";
        });
        threadPool.submit(futureTask2);

        System.out.println(futureTask1.get());
        System.out.println(futureTask2.get());

        try {TimeUnit.MILLISECONDS.sleep(300);}catch (InterruptedException e) {e.printStackTrace();}
        long endTime = System.currentTimeMillis();

        System.out.println("---costTime: " + (endTime - startTime) + "毫秒");
        threadPool.shutdown();
    }



    @Test
    public void m1() {
        long startTime = System.currentTimeMillis();
        try {TimeUnit.MILLISECONDS.sleep(500);}catch (InterruptedException e) {e.printStackTrace();}
        try {TimeUnit.MILLISECONDS.sleep(300);}catch (InterruptedException e) {e.printStackTrace();}
        try {TimeUnit.MILLISECONDS.sleep(300);}catch (InterruptedException e) {e.printStackTrace();}
        long endTime = System.currentTimeMillis();

        System.out.println("---costTime: " + (endTime - startTime) + "毫秒");
        System.out.println(Thread.currentThread().getName()+"\t ---end");
    }
}
